﻿-- =============================================
-- Author:		zengfei
-- Create date: 09m/09/2011
-- Description:	操作客户资料表
-- =============================================
Create PROCEDURE [dbo].[Usp_ModifyCustomer]
	@p_Action	char,				-- A:新增, C:修改, D:删除
	@p_CustomerNo	varchar(8),
	@p_CustomerName	varchar(200),
	@p_ShortName	varchar(60),
	@p_Contact	varchar(20),
	@p_ContactTitle	varchar(20),
	@p_Tel	varchar(15),
	@p_Mobile	varchar(15),
	@p_Fax	varchar(15),
	@p_Address	varchar(200),
	@p_ZipCode	varchar(6),
	@p_BankAccount	varchar(30),
	@p_Bank	varchar(60),
	@p_Credit	numeric(15,2),
	@p_CreditPeriod	int,
	@p_TaxAccount	varchar(30),
	@p_Region	varchar(30),
	@p_CustomerType	int,
	@p_Remark	varchar(100),
	@p_RecordID	int out,
	@p_ErrMsg	varchar(100) out,
	@p_RFlag	smallint out
AS
if	@p_Action = 'A'
begin
	if	exists (select * from Customer where CustomerNo = @p_CustomerNo)
	begin
		raiserror('客户编号[%s]重复!', 16, 1, @p_CustomerNo)
		goto ErrHandle
	end
	
	insert into Customer (CustomerNo, CustomerName, ShortName, Contact, ContactTitle, Tel, Mobile, Fax, [Address], ZipCode, BankAccount, Bank, Credit, CreditPeriod, TaxAccount, Region, CustomerType, Remark)
		values (@p_CustomerNo, @P_CustomerName, @p_ShortName, @p_Contact, @p_ContactTitle, @P_Tel, @P_Mobile, @p_Fax, @p_Address,@p_ZipCode, @p_BankAccount, @p_Bank, @p_Credit, @p_CreditPeriod, @p_TaxAccount, @p_Region, @p_CustomerType, @p_Remark)
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
	
	select @p_RecordID = @@IDENTITY 
end
else if	@p_Action = 'C'
begin
	update Customer set CustomerName = @p_CustomerName, 
						ShortName = @p_ShortName, 
						Contact = @p_Contact, 
						ContactTitle = @p_ContactTitle, 
						Tel = @p_Tel, 
						Mobile = @p_Mobile, 
						Fax = @p_Fax, 
						[Address] = @p_Address, 
						ZipCode = @p_ZipCode, 
						BankAccount = @p_BankAccount, 
						Bank = @p_Bank, 
						Credit = @p_Credit, 
						CreditPeriod = @p_CreditPeriod, 
						TaxAccount = @p_TaxAccount, 
						Region = @p_Region, 
						CustomerType = @p_CustomerType, 
						Remark = @p_Remark 
					where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end
else if	@p_Action = 'D'
begin
	Delete from Customer where RecordID = @p_RecordID 
	
	if	@@ERROR <> 0 or @@ROWCOUNT = 0 goto ErrHandle
end

set @p_RFlag = 1

ErrHandle:
set @p_RFlag = -1
